package gps.recorder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.text.format.DateFormat;
import android.widget.Toast;

public class Logger {

	private File sdCardroot;
	private File fileDir;
	private File logFile;

	public Logger() {
		sdCardroot = Environment.getExternalStorageDirectory();
		fileDir = new File(sdCardroot + "/Android/data/my.gps/files/");
		logFile = new File(fileDir + "/GPSLog.txt");
	}

	public Logger(String filename) {
		sdCardroot = Environment.getExternalStorageDirectory();
		fileDir = new File(sdCardroot + "/Android/data/my.gps/files/");
		logFile = new File(fileDir + "/" + filename);
	}

	public void showLogContent(Context context) {
		if (logFile != null && logFile.exists()) {
			String log = "";
			try {
				FileInputStream fileIS = new FileInputStream(logFile);
				BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
				String readString = new String();
				while ((readString = buf.readLine()) != null) {
					// Log.d("line: ", readString);
					log = log.concat(readString);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Toast.makeText(context, log, Toast.LENGTH_SHORT).show();
		}
	}

	public void startNewLogfile(Context context) {
		if (logFile != null && logFile.exists()) {
			String date = (String) DateFormat.format("_yyyy-MM-dd_hh-mm-ss", new Date());
			File backup = new File(fileDir + "/GPSLog" + date + ".txt");
			logFile.renameTo(backup);
			Toast.makeText(context, "Back up " + backup.toString(), Toast.LENGTH_LONG).show();
		}
	}

	public void writeToLogfile(String info) {
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			// Something else is wrong. It may be one of many other states, but
			// all we need
			// to know is we can neither read nor write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}

		if (mExternalStorageAvailable && mExternalStorageWriteable) {
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			} else if (!logFile.exists()) {
				try {
					logFile.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			String date = (String) DateFormat.format("yyyy-MM-dd hh:mm:ss ", new Date());
			info = date + info + "\n";
			try {
				FileOutputStream fos = new FileOutputStream(logFile, true);
				fos.write(info.getBytes());
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
